package com.chenglu.android.index.mvp.presenter;





import android.accounts.NetworkErrorException;

import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.index.bean.BannerBeanList;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.index.bean.NearbyShopBean;
import com.chenglu.android.index.bean.PageListBean;
import com.chenglu.android.index.bean.TabTitleBean;
import com.chenglu.android.index.mvp.contract.IndexContract;
import com.chenglu.android.index.mvp.model.IndexModel;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.google.android.material.tabs.TabLayout;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class IndexPresenter extends BasePresenter<IndexContract.View> implements IndexContract.IndexPresenter {

     private  IndexContract.IndexModel model;

    public IndexPresenter(){
        model = new IndexModel();
    }

    @Override
    public void getBannerList (String shopsTypeId , String showType) {

        if(!isViewAttached()){
            return;
        }

        model.getBannerList(shopsTypeId,showType)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<BannerBeanList>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull BannerBeanList bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getBannerList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                          mView.dismissLoading();
                          ToastUtils.show(e.getMessage());
                          LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getPageList(String level, String isIdxShow) {
        if(!isViewAttached()){
            return;
        }

        model.getPageList(level,isIdxShow)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PageListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PageListBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getPageList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getNearbyShop(String lng, String lat) {
        if(!isViewAttached()){
            return;
        }

        model.getNearbyShop(lng,lat)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<NearbyShopBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull NearbyShopBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getNearbyShop(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getIndexRecom(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }

        model.getIndexRecom(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getIndexRecom(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }



    @Override
    public void getIndexSell(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }

        model.getIndexSell(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getIndexSell(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getTabTitle() {
        if(!isViewAttached()){
            return;
        }
        model.getTabTitle()
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<TabTitleBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull TabTitleBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getTabTitle(bean);
                        }else {
                            mView.getTabTitle(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                       NetErrorException.apiError(e,mView);
                        //记得上线删除
//                        ToastUtils.show(e.getMessage());
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getTabTitleData(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getTabTitleData(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()== Constant.HTTPCODE200){
                            mView.getTabTitleData(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        //记得上线删除
//                        ToastUtils.show(e.getMessage());
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

}
